# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=libpsml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.1.0
pkgrel=1
pkgdesc="Library to process PSML pseudopotential files. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.com/siesta-project/libraries/libpsml'
msys2_references=(
  'aur: libpsml'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-xmlf90"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://gitlab.com/siesta-project/libraries/libpsml/-/archive/${pkgver}/${_realname}-${pkgver}.tar.bz2"
        "001-fix-install-dll.patch")
sha256sums=('e531270ae6daeabca713405b7e4b69ddfe8c552aca23d367cee19adcfbbb8d05'
            'ce532469cfb28d55c2b65fbb245b2a17150c0fde71958ef40bebe1309bf960e1')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/001-fix-install-dll.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|\$\{PACKAGE_PREFIX_DIR\}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/cmake/libpsml/libpsml-config.cmake
}
